﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Manage_UploadMaterials : System.Web.UI.Page
{
    protected List<Annex> annexes;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string tid = Request.Params["tid"];
            string aid = Request.Params["aid"];
            CmsDataContext db = new CmsDataContext();
            if (tid != null && tid != "")
            {
                TrainID.Value = tid;

                Train t = (from aT in db.Train
                           where aT.ID == tid
                           select aT).First();

                ArticleTitle.Value = t.Name;
                ArticleBody.Text = string.Format("<p>本次培训的主要内容是：</p>" +
                    "<p>{0}</p>" +
                    "<hr/><p>以下是本次培训的在线资源：</p>" +
                    "<p>在线资源1</p><p>在线资源2</p><p>...</p><p>在线资源n</p>" +
                    "<hr/><p>以下是本次培训的视频资源：</p>" +
                    "<p>视频资源1</p><p>视频资源2</p><p>...</p><p>视频资源n</p>" +
                    "<hr/><p>其它资源，如课件、文字材料等，请查看附件。</p>", t.Memo);
            }
            else if (aid != null && aid != null)
            {
                ArticleID.Value = aid;

                Article article = (from aA in db.Article
                                   where aA.ID == aid
                                   select aA).First();

                ArticleID.Value = article.ID;
                ArticleBody.Text = article.Body;

                annexes = article.Annex.ToList();


                #region 设置按钮状态
                //SelectAnnex.Visible = (article.CreatorID == Toolkit.CurrentUserID) && (article.Verified == null);
                //Save.Visible = (article.CreatorID == Toolkit.CurrentUserID) && (article.Verified == null);
                //VerifyArticle.Visible = (article.Verified == null) && Toolkit.HasPower(Powers.ArticleVerifier);
                #endregion
            }
        }
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();
        string aid = ArticleID.Value.Trim();
        Article article;

        try
        {
            if (aid == "")
            {
                article = new Article();
                article.ID = Toolkit.NextUUID;
                article.CreatorID = Convert.ToString(Session["ManagerID"]);
                article.TopicID = Toolkit.TopicIdOfTrainMaterial;
                article.Title = Toolkit.ArticlePrefixOfTrain + ArticleTitle.Value.Trim();
                article.Subtitle = article.Title;
                article.Keywords = "";
                article.Published = DateTime.Now;
                article.Clicked = 0;
                article.Template = Toolkit.TemplateOfTrainArticle;
                article.Footer = "";
                article.Kind = (int)ArticleKind.Normal;
                article.District = Toolkit.CurrentUserDistrict;
                article.Belong = Toolkit.CurrentUserBelong;

                article.VerifyID = Toolkit.CurrentUserID;
                article.Verified = DateTime.Now;
                
                db.Article.InsertOnSubmit(article);

                Train t = (from aT in db.Train
                           where aT.ID == TrainID.Value.Trim()
                           select aT).First();
                t.ArticleID = article.ID;
            }
            else
            {
                article = (from aA in db.Article
                     where aA.ID == aid
                     select aA).First();
            }

            article.Body = ArticleBody.Text.Trim();

            DateTime dt = article.Published;
            int year = dt.Year;
            int month = dt.Month;

            article.Url = string.Format("{0}/{1}/{2}.html", year, month, article.ID);

            string aids = AnnexID.Value.Trim();
            if (aids != "")
            {
                string[] ids = aids.Split('|');

                #region 将aids中没有的附件删除
                var q = from aA in db.Annex
                        where !ids.Contains(aA.ID) && aA.ArticleID == article.ID
                        select aA;

                foreach (string url in q.Select(aA => aA.Url))
                {
                    if (File.Exists(string.Format("{0}/{1}", Server.MapPath(Toolkit.PathOfUpload),url)))
                    {
                        File.Delete(string.Format("{0}/{1}", Server.MapPath(Toolkit.PathOfUpload), url));
                    }
                }
                db.Annex.DeleteAllOnSubmit(q);
                #endregion

                #region 选出文章的附件
                q = from aA in db.Annex
                    where ids.Contains(aA.ID)
                    select aA;

                foreach (Annex ann in q)
                {
                    ann.ArticleID = article.ID;
                }
                #endregion
            }

            db.SubmitChanges();

            Toolkit.StaticArticle(article.Url, article.Template);

            MessageBox.Message = "资料上传成功。";
            MessageBox.Location = "./UploadMaterials.aspx?aid=" + article.ID;
        }
        catch (Exception ee)
        {
            MessageBox.Message = ee.Message;
        }
    }
    protected void VerifyArticle_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();

        try
        {
            Article article = (from aA in db.Article
                               where aA.ID == ArticleID.Value.Trim()
                               select aA).First();

            article.VerifyID = Toolkit.CurrentUserID;
            article.Verified = DateTime.Now;

            db.SubmitChanges();

            MessageBox.Message = "文章已经通过审核。";
            MessageBox.Location = "./EditArticle.aspx?aid=" + article.ID;
        }
        catch (Exception ee)
        {
            MessageBox.Message = ee.Message;
        }
    }
}